package com.duubee.btframe.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class BtEncryptUtil {

    public static String md5(String str){
        String hexStr = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(str.getBytes(StandardCharsets.UTF_8));
            hexStr = BtByteUtil.bytesToHexString(digest);
        }catch (Exception e){
        }
        return hexStr;
    }


    public static class AES{
        private static final String KEY_ALGORITHM = "AES";
        private static final String CHAR_SET = "UTF-8";
        /**
         * AES的密钥长度
         */
        private static final Integer SECRET_KEY_LENGTH = 128;
        /**
         * 加解密算法/工作模式/填充方式
         */
        private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

        /**
         * AES加密操作
         *
         * @param content  待加密内容
         * @param password 加密密码
         * @return 返回Base64转码后的加密数据
         */
        public static String encrypt(String content, String password) {
            if (BtStringUtil.isEmpty(content) || BtStringUtil.isEmpty(password)) {
                return null;
            }
            try {
                //创建密码器
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
                byte[] byteContent = content.getBytes(CHAR_SET);
                //初始化为加密密码器
                cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
                byte[] encryptByte = cipher.doFinal(byteContent);
                return Base64.encodeBase64String(encryptByte);
            } catch (Exception e) {

            }
            return null;
        }

        /**
         * AES解密操作
         *
         * @param encryptContent 加密的密文
         * @param password       解密的密钥
         * @return
         */
        public static String decrypt(String encryptContent, String password) {
            if (BtStringUtil.isEmpty(encryptContent) || BtStringUtil.isEmpty(password)) {
                return null;
            }
            Cipher cipher = null;
            try {
                cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
                //设置为解密模式
                cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
                //执行解密操作
                byte[] result = cipher.doFinal(Base64.decodeBase64(encryptContent));
                return new String(result, CHAR_SET);
            } catch (Exception e) {
            }
            return null;
        }

        private static SecretKeySpec getSecretKey(final String password) throws NoSuchAlgorithmException {
            //生成指定算法密钥的生成器
            KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
            keyGenerator.init(SECRET_KEY_LENGTH, new SecureRandom(password.getBytes()));
            //生成密钥
            SecretKey secretKey = keyGenerator.generateKey();
            //转换成AES的密钥
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        }

        public static void main(String[] args) throws Exception {
            String str = "Hello World";
            System.out.println("str:" + str);

            String encryptStr = encrypt(str, "aa7889d3-435b-4ed2-99f9-08035661eda9");
            System.out.println("encrypt:" + encryptStr);

            String decryptStr = decrypt(encryptStr, "aa7889d3-435b-4ed2-99f9-08035661eda9");

            System.out.println("decryptStr:" + decryptStr);
        }
    }
}
